import TrackPlayer, {Event} from 'react-native-track-player';
import store from './player';

// service.js
module.exports = async function () {
  /**
   * 播放状态改变
   */
  TrackPlayer.addEventListener(Event.PlaybackState, data => {
    // const {state} = data;
    // console.log('播放状态改变', State[state]);
  });

  /**
   * 切歌
   */
  TrackPlayer.addEventListener(Event.PlaybackTrackChanged, data => {
    // console.log('切歌', data);
  });

  /**
   * 队列到底了
   */
  TrackPlayer.addEventListener(Event.PlaybackQueueEnded, data => {
    if ('track' in data) {
      console.log('队列到底了', data);
      store.skipToNext(false);
    }
  });

  /**
   * 封面
   */
  TrackPlayer.addEventListener(Event.PlaybackMetadataReceived, data => {
    // console.log('封面变化', data);
  });
  /**
   * 播放失败
   */
  TrackPlayer.addEventListener(Event.PlaybackError, async data => {
    // console.log('播放失败', data);
    global.showToast({
      title: '版权原因不能播放，自动播放下一首',
      icon: 'none',
    });
    store.skipToNext();
  });
};
